package com.streambolics.worseminton.data;

import javax.jdo.PersistenceManager;
import javax.jdo.Query;

import com.streambolics.data.Person;

public class InvitationList extends WorsemintonObjectList<Invitation>
{
    private Person _Guest;
    private Event  _Event;

    public static InvitationList load (PersistenceManager aDatabase, Event aEvent)
    {
        return new InvitationList ().doLoad (aDatabase, aEvent);
    }

    public static InvitationList load (PersistenceManager aDatabase, Person aGuest)
    {
        return new InvitationList ().doLoad (aDatabase, aGuest);
    }

    @Override
    protected void bindQueryVariables (Query aQuery)
    {
        aQuery.declareParameters ("Long p_id");
    }

    @Override
    protected String buildQueryText ()
    {
        if (_Guest != null)
        {
            return standardQueryText (Invitation.class) + " where guestId == p_id";
        }
        else if (_Event != null)
        {
            return standardQueryText (Invitation.class) + " where eventId == p_id";
        }
        else
        {
            return standardQueryText (Invitation.class);
        }
    }

    private InvitationList doLoad (PersistenceManager aDatabase, Event aEvent)
    {
        _Event = aEvent;
        delayLoad (aDatabase);
        return this;
    }

    private InvitationList doLoad (PersistenceManager aDatabase, Person aGuest)
    {
        _Guest = aGuest;
        delayLoad (aDatabase);
        return this;
    }

    @Override
    protected Object executeQuery (Query aQuery)
    {
        if (_Guest != null)
        {
            return aQuery.execute (_Guest.getId ());
        }
        else if (_Event != null)
        {
            return aQuery.execute (_Event.getId ());
        }
        else
        {
            return super.executeQuery (aQuery);
        }
    }

    public Event getEvent ()
    {
        return _Event;
    }

    public Person getGuest ()
    {
        return _Guest;
    }

}
